package com.cy.stone.framework.mybatisflex;

import com.cy.stone.common.base.BaseEntity;
import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.mybatisflex.codegen.dialect.IDialect;
import com.zaxxer.hikari.HikariDataSource;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class CodeGenerate {

    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://139.224.116.234:3306/Dylan_test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&&useInformationSchema=true");
        dataSource.setUsername("Dylan");
        dataSource.setPassword("Dylan@1+1=2");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig, IDialect.MYSQL);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();

        globalConfig.getJavadocConfig()
                .setAuthor("Wings")
                .setSince(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));

        //设置根包
//        globalConfig.getPackageConfig()
//                .setBasePackage("com.cy.stone.system.infrastructure.database");

        //设置表前缀和只生成哪些表，setGenerateTable 未配置时，生成所有表
        globalConfig.getStrategyConfig()
                .setTablePrefix("ST_")
                .setGenerateTable("ST_DICT_TYPE", "ST_DICT_DATA");

        //设置生成 entity 并启用 Lombok
//        globalConfig.enableEntity()
//                .setWithLombok(true)
//                .setClassSuffix("Entity")
//                .setSuperClass(BaseEntity.class)
//                .setWithSwagger(true)
//                .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC);
//
//        //设置生成 mapper
//        globalConfig.enableMapper()
//                .setClassSuffix("Mapper")
//                .setMapperAnnotation(true);
//
//        globalConfig.enableService()
//                .setClassPrefix("I")
//                .setClassSuffix("Service");
//
//        globalConfig.enableServiceImpl()
//                .setClassSuffix("ServiceImpl");

        globalConfig.enableTableDef()
                .setClassSuffix("TableDef");

        return globalConfig;
    }
}
